package com.itheima.controller;


import com.itheima.common.JsonResult;
import com.itheima.common.SnowFlake;
import com.itheima.pojo.client.*;
import com.itheima.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author ChenJunJie
 * @date 2022/6/18 17:23
 * 收货地址controller层
 */
@RestController
@RequestMapping(value = "/addressBook", produces = "application/json;charset=utf-8")
public class AddressBookController {

    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private AddressBookService addressBookService;

    //新增地址
    @PostMapping
    public JsonResult<String> addAddress(@RequestBody AddressAddDto addressAdd, HttpServletRequest request){

        String token = request.getHeader("AuthorizationUser");
        String user = redisTemplate.opsForValue().get(token);
        addressAdd.setCreateUser(user);
        addressAdd.setUserId(user);

        boolean b = addressBookService.addAddress(addressAdd);


        if (b){
            return JsonResult.success("添加地址成功");
        }else {
            return JsonResult.error("添加地址失败");
        }
    }

    //查询所有地址
    @GetMapping("/list")
    public JsonResult<List<SelectAddressVo>> selectAddress (HttpServletRequest request){

        String token = request.getHeader("AuthorizationUser");
        String userId = redisTemplate.opsForValue().get(token);

        List<SelectAddressVo> selectAddressVos = addressBookService.selectAddress(userId);

        if (selectAddressVos!=null){
            return JsonResult.select(selectAddressVos,"查询成功");
        }else {
            return JsonResult.error("暂无数据");
        }
    }

    //设置默认收货地址
    @PutMapping("/default")
    public JsonResult<SelectDefaultAddressVo> updateDefaultAddress(@RequestBody UpdateDefaultAddressDto defaultAddressDto ,HttpServletRequest request) {


        String token = request.getHeader("AuthorizationUser");
        String user = redisTemplate.opsForValue().get(token);
        defaultAddressDto.setUpdateUser(user);

        SelectDefaultAddressVo selectDefaultAddressVo = addressBookService.updateDefaultAddress(defaultAddressDto);

        if (selectDefaultAddressVo!=null){
            return JsonResult.select(selectDefaultAddressVo, "修改成功");
        }else {
            return JsonResult.error("修改失败");
        }
    }

    //查询默认收货地址
    @GetMapping("/default")
    public JsonResult<SelectDefaultAddressVo> selectDefaultAddress() {
        SelectDefaultAddressVo selectDefaultAddressVo = addressBookService.selectDefaultAddress();

        if (selectDefaultAddressVo!=null){
            return JsonResult.select(selectDefaultAddressVo, "查询成功");
        }else {
            return JsonResult.error("暂无默认收获地址");
        }
    }

    //根据id获取收货地址
    @GetMapping("/{id}")
    public JsonResult<SelectAddressByIdVo> selectAddressById(@PathVariable String id){

        SelectAddressByIdVo selectAddressByIdVo = addressBookService.selectAddressById(id);

        if (selectAddressByIdVo!=null){
            return JsonResult.select(selectAddressByIdVo, "查询成功");
        }else {
            return JsonResult.error("暂无数据");
        }
    }

    //修改收货地址
    @PutMapping
    public JsonResult<String> updateAddressById(@RequestBody UpdateAddressByIdDto updateAddressById,HttpServletRequest request){

        String token = request.getHeader("AuthorizationUser");
        String user = redisTemplate.opsForValue().get(token);
        updateAddressById.setUpdateUser(user);

        boolean b = addressBookService.updateAddressById(updateAddressById);

        if (b){
            return JsonResult.success("修改成功");
        }else {
            return JsonResult.error("修改失败");
        }
    }

    //删除收货地址
    @DeleteMapping
    public JsonResult<String> deleteAddressById(@RequestParam("ids") String id){
        boolean b = addressBookService.deleteAddressById(id);

        if (b){
            return JsonResult.success("删除成功");
        }else {
            return JsonResult.error("删除失败");
        }
    }


}
